//
//  SelectItem.m
//   CareRing
//
//  Created   on 2023/7/24.
//

#import "SelectItem.h"
#import <Masonry/Masonry.h>
#import "ConfigModel.h"
#import "Colors.h"
//#import "UIView+LayoutDirection.h"

const CGFloat HOR_MARGIN = 10.0f;

@implementation SelectItem
{
    SelectItemColorManger *_colorManager;
}
-(instancetype)initWithStyle:(ITEM_STYLE)style
{
    if (self = [super init]) {
        _colorManager = [SelectItemColorManger new];
        
        
        [self layoutBase];
        
        self.style = style;
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
        [self addGestureRecognizer:tap];
        
    }
    return self;
}

-(void)tap:(UITapGestureRecognizer *)gest {
    if (self.clickBLK) {
        self.clickBLK(self);
    }
}

-(void)layoutBase {
    
    [self addSubview:self.leftLbl];
    [self addSubview:self.selectImgView];
    
    [self.leftLbl mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.mas_centerY);
        make.leading.equalTo(self.mas_leading).offset(HOR_MARGIN);
        make.trailing.lessThanOrEqualTo(self.selectImgView.mas_leading).inset(5);
    }];
    
    [self.selectImgView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.mas_centerY);
        make.trailing.equalTo(self.mas_trailing).inset(HOR_MARGIN);
        make.width.equalTo(self.selectImgView.mas_height);
        make.height.equalTo(@20);
    }];
    
    
}

-(void)setStyle:(ITEM_STYLE)style
{
    _style = style;
    
    switch (style) {
        case NORMAL_STYLE:
        {
            _colorManager.unselecBgColor = UIColor.clearColor;
            _colorManager.selectedBgColor = UIColor.clearColor;
            _colorManager.unselecTxtColor = UIColor.whiteColor;
            _colorManager.selectedTxtColor = UIColor.whiteColor;
            [self.rightLbl removeFromSuperview];

        }
            break;
        case TITLE_STYLE:
        {
            _colorManager.unselecBgColor = UIColor.clearColor;
            _colorManager.selectedBgColor = _colorManager.unselecBgColor;
            _colorManager.unselecTxtColor = MAIN_BLUE;
            _colorManager.selectedTxtColor = _colorManager.unselecTxtColor;
            [self.rightLbl removeFromSuperview];
        }
            break;
        case DESC_STYLE:
        {
            _colorManager.unselecBgColor = UIColor.clearColor;
            _colorManager.selectedBgColor = _colorManager.unselecBgColor;
            _colorManager.unselecTxtColor = UIColor.lightGrayColor;
            _colorManager.selectedTxtColor = _colorManager.unselecTxtColor;
            
            [self.rightLbl removeFromSuperview];
        }
            break;
        case REMRK_STYLE:
        {
            _colorManager.unselecBgColor = UIColor.clearColor;
            _colorManager.selectedBgColor = ITEM_BG_COLOR;
            _colorManager.unselecTxtColor = UIColor.whiteColor;
            _colorManager.selectedTxtColor = MAIN_BLUE;
            
            
            [self addSubview:self.rightLbl];
            [self.rightLbl mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(self.mas_centerY);
                make.trailing.equalTo(self.mas_trailing).inset(HOR_MARGIN);
            }];
            self.rightLbl.textColor = _colorManager.unselecTxtColor;
        }
            break;
            
        case MORE_STYLE:
        {
            _colorManager.unselecBgColor = UIColor.clearColor;
            _colorManager.selectedBgColor = UIColor.clearColor;
            _colorManager.unselecTxtColor = UIColor.whiteColor;
            _colorManager.selectedTxtColor = UIColor.whiteColor;
            [self.rightLbl removeFromSuperview];
            self.selectImgView.image = [UIImage imageNamed:@"arrow_right_blue"]; // 箭头
            self.selectImgView.hidden = NO;
        }
            break;
        case REMRK_MORE_STYLE:
        {
            _colorManager.unselecBgColor = UIColor.clearColor;
            _colorManager.selectedBgColor = ITEM_BG_COLOR;
            _colorManager.unselecTxtColor = UIColor.whiteColor;
            _colorManager.selectedTxtColor = MAIN_BLUE;
            
            [self.selectImgView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(self.mas_centerY);
                make.trailing.equalTo(self.mas_trailing).inset(HOR_MARGIN);
                make.width.equalTo(self.selectImgView.mas_height);
                make.height.equalTo(@35);
            }];
            
            self.selectImgView.image = [UIImage imageNamed:@"exe_arrow_right"];
            self.selectImgView.hidden = NO;
            
            
            [self addSubview:self.rightLbl];
            [self.rightLbl mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(self.mas_centerY);
                make.trailing.equalTo(self.selectImgView.mas_leading).inset(5);
            }];
            self.rightLbl.textColor = _colorManager.unselecTxtColor;
        }
            break;
        default:
            break;
    }
    
    self.leftLbl.textColor = _colorManager.unselecTxtColor;
    self.backgroundColor = _colorManager.unselecBgColor;
    
    
    
}



-(void)addBottomSepLine:(UIColor *)lineColor Width:(CGFloat)width {
    
    UIView *lineView = [UIView new];
    lineView.backgroundColor = lineColor;
    [self addSubview:lineView];
    [lineView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self);
        make.height.equalTo(@(width));
        make.bottom.equalTo(self.mas_bottom);
        
    }];
    
}

-(void)setLeftText:(NSString *)text {
        
    self.leftLbl.text = text;
    
}

-(void)setRightText:(NSString *)text {
        
    self.rightLbl.text = text;
    
}



-(void)setSelected:(BOOL)selected
{
    _selected = selected;
    
    if (self.style != NORMAL_STYLE) {
        return;
    }
    
    if (selected) {
        
        self.backgroundColor = _colorManager.selectedBgColor;
        self.leftLbl.textColor = _colorManager.selectedTxtColor;
        self.selectImgView.hidden = NO;
        
        return;
    }
    
    if (!selected) {
        self.backgroundColor = _colorManager.unselecBgColor;
        self.leftLbl.textColor = _colorManager.unselecTxtColor;
        self.selectImgView.hidden = YES;

        return;
    }
    
    
}

-(SelectItemColorManger *)itemColorManager {
    return _colorManager;
}

-(UILabel *)leftLbl
{
    if (!_leftLbl) {
        _leftLbl = [UILabel new];
        _leftLbl.textAlignment =  NSTextAlignmentLeft;
        _leftLbl.font = [UIFont systemFontOfSize:18];
        _leftLbl.numberOfLines = 0;
        _leftLbl.adjustsFontSizeToFitWidth = YES;
        
    }
    return _leftLbl;
}

-(UILabel *)rightLbl
{
    if (!_rightLbl) {
        _rightLbl = [UILabel new];
        _rightLbl.textAlignment = NSTextAlignmentRight;
        _rightLbl.font = [UIFont fontWithName:FONT_ARIAL_BOLD size:18];
    }
    return _rightLbl;
}

-(UIImageView *)selectImgView
{
    if (!_selectImgView) {
        _selectImgView = [UIImageView new];
        _selectImgView.contentMode = UIViewContentModeScaleAspectFit;
        _selectImgView.image = [UIImage imageNamed:@"gou"];
        _selectImgView.hidden = YES;
    }
    return _selectImgView;
    
}






@end



@implementation  SelectItemColorManger



@end
